package tcc.wa.view.activity;

import java.util.List;

import tcc.wa.R;
import tcc.wa.model.AlertType;
import tcc.wa.presenter.AlertEnrollmentPresenter;
import tcc.wa.presenter.view.AlertEnrollmentView;
import tcc.wa.presenter.view.item.HoursInAdvanceViewItem;
import tcc.wa.view.activity.base.ActivityBase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Activity class for Alert enrollment.
 */
public class AlertEnrollmentActivity extends ActivityBase implements AlertEnrollmentView {

	private final AlertEnrollmentPresenter presenter;
	
	private Spinner typeSpinner;
	
	private EditText valueEditText;
	
	private TextView valueTextView;
	
	private TextView valueUnitTextView;
	
	private Spinner hoursIndAdvanceSpinner;
	
	public AlertEnrollmentActivity() {
		presenter = new AlertEnrollmentPresenter(this);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		presenter.onCreate();
	}
	
	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.View#initView(int)
	 */
	public void initView(int layoutId) {
		setContentView(layoutId);
		typeSpinner = (Spinner) findViewById(R.id.alertTypeSpinner);
		valueEditText = (EditText) findViewById(R.id.alertValueEditText);
		valueTextView = (TextView) findViewById(R.id.alertValueTextView);
		valueUnitTextView = (TextView) findViewById(R.id.alertValueUnitTextView);
		hoursIndAdvanceSpinner = (Spinner) findViewById(R.id.alertHoursInAdvanceSpinner);
		typeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
		        typeSpinnerItemSelected();
		    } 

		    public void onNothingSelected(AdapterView<?> adapterView) {
		        return;
		    } 
		}); 
	}
	
	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.AlertEnrollmentView#setAlertTypeList(java.util.List)
	 */
	public void setAlertTypeList(List<AlertType> alertTypes) {
		ArrayAdapter<AlertType> adapter = new ArrayAdapter<AlertType>(this, android.R.layout.simple_spinner_item, alertTypes);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		typeSpinner.setAdapter(adapter);
	}
	
	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.AlertEnrollmentView#setHoursInAdvanceList(java.util.List)
	 */
	public void setHoursInAdvanceList(List<HoursInAdvanceViewItem> hoursInAdvance) {
		ArrayAdapter<HoursInAdvanceViewItem> adapter = new ArrayAdapter<HoursInAdvanceViewItem>(this, android.R.layout.simple_spinner_item, hoursInAdvance);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		hoursIndAdvanceSpinner.setAdapter(adapter);
	}
	
	/* (non-Javadoc)
	 * @see tcc.wa.presenter.view.AlertEnrollmentView#finishEnrollment()
	 */
	public void finishEnrollment() {
		finish();
	}
	
	/**
	 * Creates an alert.
	 *
	 * @param view the view of origin
	 */
	public void createAlertButtonClick(View view) {
		if (valueEditText.getText().length() == 0) {
			showToast("Todos os campos devem ser preenchidos");
		} else {
			AlertType type = (AlertType) typeSpinner.getSelectedItem();
			double value = Double.parseDouble(valueEditText.getText().toString());
			int hoursInAdvance = ((HoursInAdvanceViewItem) hoursIndAdvanceSpinner.getSelectedItem()).getValue();
			presenter.createAlert(type, value, hoursInAdvance);
		}
	}
	
	/**
	 * Cancels the alert enrollment process.
	 *
	 * @param view the view of origin
	 */
	public void cancelAlertEnrollmentButtonClick(View view) {
		presenter.cancelAlertEnrollment();
	}
	
	/**
	 * Changes some of the text whenever the selected AlertType changes.
	 */
	private void typeSpinnerItemSelected() {
		AlertType type = (AlertType) typeSpinner.getSelectedItem();
		switch (type) {
		case HIGH_TEMPERATURE:
			valueTextView.setText(getResources().getString(R.string.above));
			valueUnitTextView.setText(getResources().getString(R.string.celsius_degree));
			break;
		case LOW_TEMPERATURE:
			valueTextView.setText(getResources().getString(R.string.below));
			valueUnitTextView.setText(getResources().getString(R.string.celsius_degree));
			break;
		case RAIN:
			valueTextView.setText(getResources().getString(R.string.above));
			valueUnitTextView.setText(getResources().getString(R.string.milimeter));
			break;
		default:
			valueTextView.setText(getResources().getString(R.string.alert_value));
			valueUnitTextView.setText("");
		}
	}
}
 